package com.whfc.ms.api.emp.controller;

import com.whfc.common.base.BaseController;
import com.whfc.common.result.ListData;
import com.whfc.common.result.PageData;
import com.whfc.common.result.Result;
import com.whfc.common.result.ResultUtil;
import com.whfc.common.util.SessionAttr;
import com.whfc.emp.dto.AppFaceGateConfigDTO;
import com.whfc.emp.dto.AppFaceGateDTO;
import com.whfc.emp.dto.AppFaceGateEmpDTO;
import com.whfc.emp.dto.AppFaceGatePlatformDTO;
import com.whfc.emp.param.AppFacaGateEditParam;
import com.whfc.emp.param.AppFaceGateAddParam;
import com.whfc.emp.param.AppFaceGateGrantEmpParam;
import com.whfc.fuum.entity.SysUser;
import com.whfc.ms.api.emp.service.MsAppFaceGateService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 闸机管理
 *
 * @author guodong_sun
 * @Description:
 * @date 2019年9月5日
 */
@RequiresPermissions("emp/faceGate")
@RestController
@RequestMapping("/ms/api/faceGate")
public class AppFaceGateController extends BaseController {
    @Autowired
    private MsAppFaceGateService msAppFaceGateService;

    /**
     * 获取闸机列表
     *
     * @param user
     * @param deptId
     * @param keyword
     * @return
     */
    @GetMapping("/list")
    public Result list(@SessionAttribute(SessionAttr.MS_USER) SysUser user,
                       @RequestParam("deptId") Integer deptId,
                       @RequestParam(value = "keyword", required = false) String keyword) {
        logger.info("获取闸机列表,loginer:{},deptId:{},keyword:{}", user.getUsername(), deptId, keyword);
        ListData<AppFaceGateDTO> pageData = msAppFaceGateService.list(deptId, keyword);
        return ResultUtil.success(pageData);
    }


    /**
     * 查看闸机授权人员
     *
     * @param user
     * @param faceGateId
     * @param keyword
     * @param pageNum
     * @param pageSize
     * @return
     */
    @GetMapping("/emp/list")
    public Result empList(@SessionAttribute(SessionAttr.MS_USER) SysUser user,
                          @RequestParam("faceGateId") Integer faceGateId,
                          @RequestParam(value = "keyword", required = false) String keyword,
                          @RequestParam(value = "groupId", required = false) Integer groupId,
                          @RequestParam(value = "workTypeId", required = false) Integer workTypeId,
                          @RequestParam(value = "type", required = false) Integer type,
                          @RequestParam("pageNum") Integer pageNum,
                          @RequestParam("pageSize") Integer pageSize) {
        logger.info("查看闸机授权人员，loginer:{},faceGateId:{},keyword:{},groupId:{},workTypeId:{}，type：{}，pageNum:{},pageSize:{}",
                user.getUsername(), faceGateId, keyword, groupId, workTypeId, pageNum, pageSize);
        PageData<AppFaceGateEmpDTO> pageData = msAppFaceGateService.empList(faceGateId, keyword, groupId, workTypeId, type, pageNum, pageSize);
        return ResultUtil.success(pageData);
    }


    /**
     * 添加闸机
     *
     * @param user
     * @param request
     * @return
     */
    @RequiresPermissions("emp/faceGate/add")
    @PostMapping("/add")
    public Result add(@SessionAttribute(SessionAttr.MS_USER) SysUser user, @Validated @RequestBody AppFaceGateAddParam request) {
        logger.info("添加闸机,loginer:{},AppFaceGateAddParam:{}", user.getUsername(), request.toString());
        msAppFaceGateService.add(request);
        return ResultUtil.success();
    }


    /**
     * 修改闸机
     *
     * @param user
     * @param request
     * @return
     */
    @RequiresPermissions("emp/faceGate/edit")
    @PostMapping("/edit")
    public Result edit(@SessionAttribute(SessionAttr.MS_USER) SysUser user, @Validated @RequestBody AppFacaGateEditParam request) {
        logger.info("修改闸机,loginer:{},AppFacaGateEditParam:{}", user.getUsername(), request.toString());
        msAppFaceGateService.edit(request);
        return ResultUtil.success();
    }


    /**
     * 删除闸机
     *
     * @param user
     * @param faceGateId
     * @return
     */
    @RequiresPermissions("emp/faceGate/del")
    @PostMapping("/del/{faceGateId}")
    public Result del(@SessionAttribute(SessionAttr.MS_USER) SysUser user, @PathVariable("faceGateId") Integer faceGateId) {
        logger.info("删除闸机,loginer:{},faceGateId:{}", user.getUsername(), faceGateId);
        msAppFaceGateService.del(faceGateId);
        return ResultUtil.success();
    }


    /**
     * 闸机-添加人员授权
     *
     * @param user
     * @param request
     * @return
     */
    @RequiresPermissions("emp/faceGate/addEmps")
    @PostMapping("/emp/grant")
    public Result facaGateGrantEmp(@SessionAttribute(SessionAttr.MS_USER) SysUser user, @Validated @RequestBody AppFaceGateGrantEmpParam request) {
        logger.info("闸机-添加人员授权,loginer:{},AppFaceGateGrantEmpParam:{}", user.getUsername(), request.toString());
        msAppFaceGateService.faceGateGrantEmp(request);
        return ResultUtil.success();
    }


    /**
     * 闸机-取消人员授权
     *
     * @param user
     * @param request
     * @return
     */
    @RequiresPermissions("emp/faceGate/delEmps")
    @PostMapping("/emp/revoke")
    public Result faceGateRevokeEmp(@SessionAttribute(SessionAttr.MS_USER) SysUser user, @Validated @RequestBody AppFaceGateGrantEmpParam request) {
        logger.info("闸机-取消人员授权,loginer:{},AppFaceGateGrantEmpParam:{}", user.getUsername(), request.toString());
        msAppFaceGateService.faceGateRevokeEmp(request);
        return ResultUtil.success();
    }


    /**
     * 获取未授权的人员
     *
     * @param user
     * @param faceGateId
     * @param keyword
     * @param pageNum
     * @param pageSize
     * @return
     */
    @GetMapping("/emp/unGrantList")
    public Result unGrantEmpList(@SessionAttribute(SessionAttr.MS_USER) SysUser user,
                                 @RequestParam("faceGateId") Integer faceGateId,
                                 @RequestParam(value = "keyword", required = false) String keyword,
                                 @RequestParam(value = "groupId", required = false) Integer groupId,
                                 @RequestParam("pageNum") Integer pageNum,
                                 @RequestParam("pageSize") Integer pageSize,
                                 @RequestParam("deptId") Integer dedptId) {
        logger.info("获取未授权的人员,loginer:{},faceGateId:{},keyword:{},groupId:{},pageNum:{},pageSize:{}", user.getUsername(), faceGateId, keyword, groupId, pageNum, pageSize);
        PageData<AppFaceGateEmpDTO> pageData = msAppFaceGateService.unGrantEmpList(dedptId, faceGateId, groupId, keyword, pageNum, pageSize);
        return ResultUtil.success(pageData);
    }


    /**
     * 获取授权失败的人员
     *
     * @param user
     * @param faceGateId
     * @param keyword
     * @param pageNum
     * @param pageSize
     * @return
     */
    @GetMapping("/emp/failList")
    public Result failList(@SessionAttribute(SessionAttr.MS_USER) SysUser user,
                           @RequestParam("faceGateId") Integer faceGateId,
                           @RequestParam(value = "keyword", required = false) String keyword,
                           @RequestParam(value = "groupId", required = false) Integer groupId,
                           @RequestParam("pageNum") Integer pageNum,
                           @RequestParam("pageSize") Integer pageSize) {
        logger.info("获取授权失败的人员,loginer:{},faceGateId:{},keyword:{},groupId:{},pageNum:{},pageSize:{}", user.getUsername(), faceGateId, keyword, groupId, pageNum, pageSize);
        PageData<AppFaceGateEmpDTO> pageData = msAppFaceGateService.failList(faceGateId, groupId, keyword, pageNum, pageSize);
        return ResultUtil.success(pageData);
    }

    @PostMapping("/sync/{faceGateId}")
    public Result sync(@SessionAttribute(SessionAttr.MS_USER) SysUser user,
                       @PathVariable("faceGateId") Integer faceGateId) {
        logger.info("同步闸机授权人员,loginer:{},faceGateId:{}", user.getUsername(), faceGateId);
        msAppFaceGateService.sync(faceGateId);
        return ResultUtil.success();
    }

    @RequiresPermissions("emp/faceGate/openDoor")
    @GetMapping("/openDoor")
    public Result openDoor(@SessionAttribute(SessionAttr.MS_USER) SysUser user,
                           @RequestParam("faceGateId") Integer faceGateId) {
        logger.info("闸机开门,loginer:{},faceGateId:{}", user.getUsername(), faceGateId);
        msAppFaceGateService.openDoor(faceGateId);
        return ResultUtil.success();
    }


    /**
     * 获取接入过能添加的闸机
     *
     * @param user
     * @param deptId
     * @return
     */
    @GetMapping(value = "/platformList", params = {"deptId"})
    public Result platformList(@SessionAttribute(SessionAttr.MS_USER) SysUser user,
                               @RequestParam(value = "deptId", required = false) Integer deptId) {
        logger.info("获取接入过能添加的闸机,loginer:{},deptId:{}", user.getUsername(), deptId);
        List<AppFaceGateConfigDTO> list = msAppFaceGateService.getFaceGateConfigList(deptId);
        return ResultUtil.success(new ListData<>(list));
    }


    @GetMapping(value = "/platformList")
    public Result platformList(@SessionAttribute(SessionAttr.MS_USER) SysUser user) {
        logger.info("获取闸机平台列表,loginer:{}", user.getUsername());
        List<AppFaceGatePlatformDTO> list = msAppFaceGateService.getFaceGatePlatformList();
        return ResultUtil.success(new ListData<>(list));
    }
}
